<?php
/*
 * Function:File cache
 * Project :SuperEMall
 *
 * Dreamxyp(QQ:31996798) - GZ - PHP - cache.class.php
 * Coding  :xiayiping[xiayiping_xyp@163.com]
 * Created on 2007-12-20
 */
class ca
{
	protected $key,$dir,$time,$level,$filename;
	
	protected $fileDir  = DATACACHE_DIR;
		
	private $cache;
	
	public function __construct($key,$dir="array",$time=300,$level=0,$fileDir=false)
	{
		$this->key		=$key;
		$this->dir 		=$dir;
		$this->time		=$time;
		$this->level	=$level;
		if($fileDir)
			$this->fileDir = $fileDir;
		$this->getFileName($key);
	}																																																																																																															
	
	public function get()
	{
		if($this->cache)
		{
		    return $this->cache;
		}else
		{
		   if(file_exists($this->filename) && (!$this->time || filemtime($this->filename)>time()-$this->time) && filesize($this->filename)>4)
		    {
		        $rs = array();
		        @include $this->filename;
		        $this->cache = $rs?$rs:false;
		        return $this->cache;
		    }else
		    {
		        return false;
		    }
		}
	}
	/*
	 * 功能:寫
	 */
	function put($var,$key=null)
	{
		if($key)
		{
		    $rs = $this->get();
		    $rs[$key] = $var;
		    $this->cache = $rs;
			$rs = $this->envalue($rs);
		}else
		{
		    $this->cache = $var;
		    $rs = $this->envalue($var);
		}
		return $this->write($this->filename,"<"."? \$rs={$rs}; ?".">");
	}
	/*
	 * 功能:刪
	 */
	public function del()
	{
		$this->cache = false;
		if(file_exists($this->filename))unlink($this->filename);
	}
	/*
	 * 功能:得到文件名
	 */
	public function getFileName()
	{
		if($this->filename)
			return $this->filename;
		$md5 = abs(crc32($this->key));
		if($this->level<=5)
		{
			$this->filename =  $this->fileDir.$this->dir."/";  
		}elseif($this->level<=30)
		{
		    $VarOrd1 = $md5%30;
		    $this->filename =  $this->fileDir.$this->dir."/".$VarOrd1."/"; 
		}elseif($this->level<=300)
		{
		    $VarOrd1 = substr($md5,-2,1);	
			$VarOrd2 = substr($md5,-1,1);
			$this->filename =  $this->fileDir.$this->dir."/".$VarOrd1."/".$VarOrd2."/"; 
		}else
		{
		    $VarOrd1 = substr($md5,-4,2);	
			$VarOrd2 = substr($md5,-2,2);
			$this->filename =  $this->fileDir.$this->dir."/".$VarOrd1."/".$VarOrd2."/";
		}
		$this->dir($this->filename,true);
		$this->filename.= urlencode($this->key);
		return $this->filename;
	}
	private function envalue($val, $level= 0)
	{
	    if (is_array($val))
	    {
	        $space= '';
	        for ($i= 0; $i < $level; $i++)
	        	$space .= "\t\t";
	        $evaluate = "array\n$space(\n";
	        $comma	  = array();
	        foreach ($val as $k => $v)
            {
                $k = is_string($k) ? '\'' . addcslashes($k, '\'\\') . '\'' : $k;
                $comma[]= "$space\t$k=> " . $this->envalue($v, $level +1);
            }
            $evaluate .= implode(",\n",$comma);
            $evaluate .= "\n$space)";
            return $evaluate;
	    }else
	    {
	        return !preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12 ? '\'' . addcslashes($val, '\'\\') . '\'' : $val;
	    }
	}
	private function write($fileName, $content, $type = "w")
	{
		$fd = fopen($fileName, $type);
		if ($fd)
		{
			$rs = fwrite($fd, $content);
			fclose($fd);
			return $rs;
		} else
		{
			return false;
		}
	}
	private function dir($dir,$recursive=false)
	{
		if (!file_exists($dir))
		{
			@ mkdir($dir, 0777,$recursive);
			if (file_exists($dir))
			{
				return true;
			} else
			{
				return false;
			}
		}else 
		{
			return true;
		}
	}
}
?>